package com.zidongxiangxi.clock.server.utils;

import com.zidongxiangxi.clock.server.repository.entity.Clock;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.Objects;

/**
 * 闹钟工具类
 *
 * @author chenxudong
 * @date 2020/07/31
 */
public class ClockUtils {

    /**
     * 下次响铃时间间隔
     */
    public static final int NEXT_TRY_RING_TIME = 20;

    /**
     * 一秒
     */
    public static final int ONE_SECOND = 1000;

    public static Long getNewNextTryTime() {
        return System.currentTimeMillis() + NEXT_TRY_RING_TIME * ONE_SECOND;
    }

    public static Clock covertToUpdateAlarmClock(Clock alarmClock) {
        Clock updateAlarmClock = new Clock();
        BeanUtils.copyProperties(alarmClock, updateAlarmClock);
        updateAlarmClock.setRetryTimes(Objects.isNull(alarmClock.getRetryTimes()) ? 0 : alarmClock.getRetryTimes() + 1);
        long nextTryTime = alarmClock.getNextTryTime() + NEXT_TRY_RING_TIME * ONE_SECOND;
        updateAlarmClock.setNextTryTime(nextTryTime);
        updateAlarmClock.setUpdateTime(new Date());
        return updateAlarmClock;
    }

}
